package com.chenzhengrong.demospringboot.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenzhengrong
 * @title: WeatherUtils
 * @description: 天气工具类
 * @date 2022/10/13 11:41
 */
@Component
public class WeatherUtils {

    // 天气地址编号
    @Value("${weather.city}")
    private String city;

    // 天气key
    @Value("${weather.key}")
    private String key;

    // 空气质量地址编号
    @Value("${weather.areacode}")
    private String areacode;

    // 空气质量X-APISpace-Token
    @Value("${weather.space.token}")
    private String spaceToken;

    /**
    * @description: 获取天气详细信息
    * @return java.util.Map<java.lang.String,java.lang.Object>
    * @author chenzhengrong
    */
    public Map<String, Object> getWeatherInfo() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("city", city);
        paramMap.put("key", key);
        String result= HttpUtil.get("https://restapi.amap.com/v3/weather/weatherInfo?parameters", paramMap);
        return JSONUtil.toBean(result, Map.class);
    }

    /**
    * @description: 获取空气质量
    * @return java.util.Map<java.lang.String,java.lang.Object>
    * @author chenzhengrong
    */
    public Map<String, Object> getWeatherQuality() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("areacode", areacode);
        String result = HttpRequest.get("https://eolink.o.apispace.com/34324/air/v001/aqi")
                .header("X-APISpace-Token", spaceToken)
                .header("Authorization-Type","apikey")//头信息，多个头信息多次调用此方法即可
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        return JSONUtil.toBean(result, Map.class);
    }

}
